/**
 *  Copyright (C) 2008-2013  Telosys project org. ( http://www.telosys.org/ )
 *
 *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *          http://www.gnu.org/licenses/lgpl.html
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.telosys.common.data;

import java.io.Serializable;


/**
 * 
 * Generic TreeNode to build objects trees
 * 
 * @author Laurent GUERIN
 *  
 */
public class TreeNode implements Serializable
{
	private static final long serialVersionUID = 1L;
	
	private final static TreeNodeList VOID_TREE_NODE_LIST = new TreeNodeList() ;
    private Object       _object       = null ;
    private String       _sId          = null ;
    private int          _iType        = 0 ;
    private TreeNodeList _children = null ;
    
    //----------------------------------------------------------------------------------------
    // Contructor(s)
    //----------------------------------------------------------------------------------------    
    /**
     * Constructs a new TreeNode
     * @param object
     */
    public TreeNode ( Object object )
    {
        super();
        _object = object ;
        _sId    = null ;
        _iType  = 0 ;
    }
    //----------------------------------------------------------------------------------------    
    /**
     * Constructs a new TreeNode with a type
     * @param object
     * @param type
     */
    public TreeNode ( Object object, int type )
    {
        super();
        _object = object ;
        _sId    = null ;
        _iType  = type ;
    }
    //----------------------------------------------------------------------------------------    
    /**
     * Constructs a new TreeNode with an id
     * @param object
     * @param id
     */
    public TreeNode ( Object object, String id )
    {
        super();
        _object = object ;
        _sId    = id ;
        _iType  = 0 ;
    }
    //----------------------------------------------------------------------------------------    
    /**
     * Constructs a new TreeNode with an id and a type
     * @param object
     * @param id
     * @param type
     */
    public TreeNode ( Object object, String id, int type )
    {
        super();
        _object = object ;
        _sId    = id ;
        _iType  = type ;
    }

    //----------------------------------------------------------------------------------------
    // Getters & Setters
    //----------------------------------------------------------------------------------------    
    /**
     * Set the object held by the node
     * @param v
     */
    public void setObject( Object v)
    {
        _object = v;
    }
    /**
     * Returns the object held by the node
     * @return
     */
    public Object getObject()
    {
        return _object;
    }
    //----------------------------------------------------------------------------------------
    /**
     * Set the type of the node
     * @param v
     */
    public void setType( int v ) 
    {
        _iType = v ;
    }
    /**
     * Returns the type of the node
     * @return
     */
    public int getType()
    {
        return _iType;
    }
    
    //----------------------------------------------------------------------------------------
    /**
     * Set the id of the node
     * @param v
     */
    public void setId( String v ) 
    {
        _sId = v ;
    }
    /**
     * Returns the id of the node
     * @return
     */
    public String getId()
    {
        return _sId;
    }
    
    //----------------------------------------------------------------------------------------
    // Child nodes management
    //----------------------------------------------------------------------------------------    
    /**
     * Returns all the children of the node ( like W3C DOM )
     * @return
     */
    public TreeNodeList getChildNodes() // like W3C DOM
    {
        if ( _children != null )
        {
            return _children;
        }
        else
        {
            return VOID_TREE_NODE_LIST;
        }
    }
    //----------------------------------------------------------------------------------------
    /**
     * Returns true if the node has children  ( like W3C DOM )
     * @return
     */
    public boolean hasChildNodes() // like W3C DOM
    {
        if ( _children != null )
        {
            return ( _children.size() > 0 ) ;
        }
        return false ;
    }
    //----------------------------------------------------------------------------------------    
    /**
     * Appends a node at the end of the children of the node ( like W3C DOM )
     * @param node
     * @return
     */
    public boolean appendChild( TreeNode node ) // like W3C DOM
    {
        if ( _children == null )
        {
            _children = new TreeNodeList();
        }
        return _children.add(node);
    }
    
    //----------------------------------------------------------------------------------------    
    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    public String toString() 
    {
        int n = ( _children != null ? _children.size() : 0 ) ;
        String s = "null" ;
        if ( _object != null )
        {
            s = _object.getClass().getName() ;
        }
        return "TreeNode [id:"+_sId+"|type:"+_iType+"|object:"+s+"|"+ n + " child node(s)]";
    }
    
}
